﻿using Org.BouncyCastle.Utilities.Encoders;
using PasswdTool.Model;
using PasswdTool.SM4;
using System;
using System.Text;
using System.Windows.Forms;
using Tables;

namespace PasswdTool
{
    public partial class DetailFrm : Form
    {
        public MainFrm MainFrm { get; set; }
        public bool IsNew { get; set; }
        public bool IsUpdate { get; set; }
        public PasswdModel PasswdModel { get; set; }
        public DetailFrm()
        {
            InitializeComponent();
        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void OkBtn_Click(object sender, EventArgs e)
        {
            if (IsNew)
            {
                string name = nameTxb.Text;
                string username = usernameTxb.Text;
                string passwd = passwdTxb.Text;
                if (string.IsNullOrEmpty(name))
                {
                    MessageBox.Show("用户不能为空!", "PasswdTool提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    nameTxb.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(username))
                {
                    MessageBox.Show("用户不能为空!", "PasswdTool提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    usernameTxb.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(passwd))
                {
                    MessageBox.Show("密码不能为空!", "PasswdTool提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    passwdTxb.Focus();
                    return;
                }
                Sm4Crypt sm4Crypt = new Sm4Crypt()
                {
                    Key = GlobalContainer.CurrentUser.Encrypt,
                    Iv = GlobalContainer.CurrentUser.IV
                };
                PasswdModel model = new PasswdModel()
                {
                    UserId = GlobalContainer.CurrentUser.Id,
                    Name = name,
                    Username = username,
                    Passwd = sm4Crypt.EncryptECB(Encoding.UTF8.GetBytes(passwd)).ToHexStr()
                };
                PasswdTable table = GlobalContainer.PasswdTable;
                table.Insert(model);
                MainFrm.LoadData();
            }
            this.Close();
        }

        private void GenerateBtn_Click(object sender, EventArgs e)
        {
            Generate generate = new Generate(lowerCkb.Checked,upperCkb.Checked,numCkb.Checked,symbolCkb.Checked);
            passwdTxb.Text = generate.GeneratePwd(int.Parse(lengthNud.Value.ToString()));
        }

        private void CopyBtn_Click(object sender, EventArgs e)
        {
            passwdTxb.SelectAll();
            passwdTxb.Copy();
            MessageBox.Show("复制成功!");
        }

        private void PasswdTxb_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (passwdTxb.ReadOnly && (IsUpdate || IsNew))
            {
                passwdTxb.ReadOnly = false;
                passwdTxb.Focus();
                passwdTxb.Select(passwdTxb.Text.Length, 0);
            }
        }

        private void PasswdTxb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                passwdTxb.ReadOnly = true;
                label3.Focus();
            }
        }

        private void DetailFrm_Load(object sender, EventArgs e)
        {
            if (!IsNew)
            {
                this.Text = PasswdModel.Name;
                nameTxb.Text = PasswdModel.Name;
                usernameTxb.Text = PasswdModel.Username;
                Sm4Crypt sm4Crypt = new Sm4Crypt()
                {
                    Key = GlobalContainer.CurrentUser.Encrypt,
                    Iv = GlobalContainer.CurrentUser.IV
                };
                passwdTxb.Text = sm4Crypt.DecryptECB(Hex.Decode(PasswdModel.Passwd)).ToString();
                if (!IsUpdate)
                {
                    nameTxb.Enabled = false;
                    usernameTxb.Enabled = false;
                    lengthNud.Enabled = false;
                    lowerCkb.Enabled = false;
                    upperCkb.Enabled = false;
                    numCkb.Enabled = false;
                    symbolCkb.Enabled = false;
                    generateBtn.Enabled = false;
                }
            }
        }
    }
}
